package com.itqf.controller.exception;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/21
 * @Time: 上午11:11
 */
@ControllerAdvice
//@RestControllerAdvice  @ControllerAdvice+@ResponseBody
public class MyExceptionHandler {

//    @ExceptionHandler(ArithmeticException.class)
//    //如果是页面 返回String或者ModelAndView
//    //如果是json  返回对象  @ResponseBody
//    public String handlerException(ArithmeticException a){
//        a.printStackTrace();
//        return "error.jsp";
//    }


    @ExceptionHandler(ArithmeticException.class)
    //如果是页面 返回String或者ModelAndView
    //如果是json  返回对象  @ResponseBody
    @ResponseBody
    public JSONObject handlerException(ArithmeticException a){
        a.printStackTrace();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",1);
        jsonObject.put("error",a.getLocalizedMessage());
        jsonObject.put("msg","操作异常");

        return jsonObject;
    }

    @ExceptionHandler(Exception.class)
    //如果是页面 返回String或者ModelAndView
    //如果是json  返回对象  @ResponseBody
    @ResponseBody
    public JSONObject handlerAllException(Exception a){
        a.printStackTrace();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",1);
        jsonObject.put("error",a.getLocalizedMessage());
        jsonObject.put("msg","操作异常");

        return jsonObject;
    }




}
